//detail.js
//获取应用实例
import S_request from '../../utils/requestService.js';
const app = getApp()
let detailType = '';
let formId = 0;//表单Id
Page({
  data: {
    detailData: {},
    shareData: null,
  },
  onLoad: function(options) {
    let gameId = options.gameId;
    this.getDetailInfo(gameId);
    // detailType = options.type;
    // console.log("detailType====" + detailType)
    // let shareData = options.data; //获取分享的数据
    // let data;
    // if (detailType == "game") {
    //   for (let i = 0; i < app.globalData.gameLists.length; i++) {
    //     let item = app.globalData.gameLists[i];
    //     for (let j = 0; j < item.list.length; j++) {
    //       let list = item.list[j];
    //       if (list.id == gameId) {
    //         list.btnTitle = '开始'
    //         data = list;
    //         break;
    //       }
    //     }
    //   }
    // } else if (detailType == "play") {
    //   for (let i = 0; i < app.globalData.playLists.length; i++) {
    //     let list = app.globalData.playLists[i];
    //     if (list.id == gameId) {
    //       list.btnTitle = '开始'
    //       data = list;
    //       break;
    //     }
    //   }
    // } else if (shareData && shareData != 'undefined') {
    //   console.log("shareDatas===" + shareData)
    //   data = JSON.parse(shareData); //获取分享的数据后,将string转成json
    //   console.log("shareData===" + data);
    // }
    // let name = data.game_name;
    // wx.setNavigationBarTitle({
    //   title: data.game_name
    // })
    // this.setData({
    //   detailData: data
    // })
  },
  getDetailInfo: function(id) {
    S_request.play.getDetailInfo(id, (data) => {
      let info = data.info
      wx.setNavigationBarTitle({
        title: info.game_name
      })
      this.setData({
        detailData: info
      })
    })
  },
  onShareAppMessage: function(res) {
    wx.showShareMenu({
      withShareTicket: true,
      success: function() {}
    });
    let data;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
      data = res.target.dataset.id;
    } else if (res.from === 'menu') {
      data = this.data.detailData;
    }
    this.shareRecord();
    let fu = wx.getStorageSync('userId')
    return {
      title: data.game_name,
      path: '/pages/index/index?gameId=' + data.id + "&fu=" + fu + "&share=4", ///pages/detail/detail?gameId=
      imageUrl: ""
    }
  },
  shareRecord: function() {
    // console.log("this.detailData=" + JSON.stringify(this.data.detailData));
    let detailData = this.data.detailData;
    let obj = { 'pagepath': "/pages/detail/detail" }
    S_request.index.recordShare(detailData.id, obj, (data) => {});
  },
  playGame: function(e) {
    let id = e.currentTarget.dataset.id;
    console.log("id====" + id);
    let param = "formId=" + formId
    S_request.index.tongJiNum(id, param,(result) => {
      console.log("data====" + result);
    })
    let path = e.currentTarget.dataset.path;
    if (path != undefined && path != '') {
      wx.navigateTo({
        url: path,
      })
    }
  },
  formSubmit: function (e) {
    formId = e.detail.formId;
    console.log('form发生了submit事件，携带数据为：', e.detail.value + ", " + e.detail.formId)
  }
})